package com.wiegand.content.contents.textcontent {
	import com.caurina.transitions.Tweener;
	import com.five3D.display.Scene3D;
	import com.five3D.display.Shape3D;
	import com.five3D.display.Sprite3D;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import flash.geom.Rectangle;
	import flash.utils.setTimeout;

	/**
	 * @author kwiegand
 	**/
public class TextScene3D extends Sprite {

		private var _xml : XMLList;
		private var _textblocks : Sprite3D;
		private var _scene : Scene3D;
		private var _background : Shape3D;
		private var _shadow : Sprite;

		public function TextScene3D(xml:XMLList){
			_xml = xml;
			init();
		}
		
		private function init() : void {
			_shadow = new Sprite();
			addChild(_shadow);
			
			_background = new Shape3D();		
			
			_textblocks = new Sprite3D();
			_textblocks.z = -300;

			_scene = new Scene3D();
			_scene.addChild(_background);
			_scene.addChild(_textblocks);
			addChild( _scene );

			for (var i : int = 0; i < _xml.children().length(); i++) {
				var block : TextBlock3D = new TextBlock3D( _xml.child(i) );
				block.z = int(_xml.child(i).@z);
				block.rotationX = int(_xml.child(i).@rotX);
				block.rotationY = int(_xml.child(i).@rotY);
				block.y = int(_xml.child(i).@y);
				block.x = int(_xml.child(i).@x);
				_textblocks.addChild(block);
			}
		}
		
		public function updateBackground(rect:Rectangle):void{
			_background.graphics3D.clear();
			_background.graphics3D.beginFill(0xff00ff, 0);
			_background.graphics3D.drawRect(rect.x, rect.y, rect.width, rect.height);
			_background.graphics3D.endFill();
		
			_textblocks.alpha = 0;
			setTimeout(makePhoto, 100);		
		}
		
		private function makePhoto() : void {
			_textblocks.alpha = 1;
			this.parent.filters = [new BlurFilter(20,20,2)];
			this.parent.alpha = 1;
			var bmpData : BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x00000000);
			bmpData.draw(this.parent);
			this.parent.filters = [];
			this.parent.alpha = 0;
			_shadow.addChild( new Bitmap(bmpData) );
			_shadow.x = -stage.stageWidth/2;
			_shadow.y = -stage.stageHeight/2;
			_shadow.alpha = 0;
			
			for (var k : int = 0; k < _xml.children().length(); k++) {
				var blockk : TextBlock3D = TextBlock3D( _textblocks.getChildAt(k) );
				blockk.explode(0);
			}			
		}

		public function open(delay:Number=0):void{
			setTimeout(openBlocks, (delay*1000) );
		}
		
		private function openBlocks():void{
			handleMouseMove();
			addEventListener( MouseEvent.MOUSE_MOVE, handleMouseMove );
			for (var i : int = 0; i < _textblocks.numChildren; i++) {
				var block : TextBlock3D = TextBlock3D( _textblocks.getChildAt(i) );
				block.assemble(4,(i*1));
			}		
			Tweener.addTween(_shadow,{alpha:.6, delay:(i*1), time:4,transition:"linear"});
		}

		public function close():void{
			removeEventListener( MouseEvent.MOUSE_MOVE, handleMouseMove );
			for (var i : int = 0; i < _textblocks.numChildren; i++) {
				var block : TextBlock3D = TextBlock3D( _textblocks.getChildAt(i) );
				block.explode(1);
			}			
			Tweener.addTween(_shadow,{alpha:0, time:.5,transition:"linear"});
		}
		

		public function destroy():void{
			removeEventListener( MouseEvent.MOUSE_MOVE, handleMouseMove );
			Tweener.removeTweens(_textblocks);
			Tweener.removeTweens(_shadow);
			removeChild( _shadow );
			removeChild( _scene );
			for (var i : int = 0; i < _textblocks.numChildren; i++) {
				var block : TextBlock3D = TextBlock3D( _textblocks.getChildAt(i) );
				block.destroy();
				block = null;
			}			
		}
		
		private function handleMouseMove( event:MouseEvent=null ): void{ 
			var moveSpeed:Number = 10;			
			Tweener.addTween( _textblocks, {
				rotationY:-( stage.stageWidth/2 - stage.mouseX )/moveSpeed,
				rotationX:( stage.stageHeight/2 - stage.mouseY )/moveSpeed,
				time:5,
				transition:"easeoutback"
			});
			Tweener.addTween( _shadow, {
				rotationY:+( stage.stageWidth/2 - stage.mouseX )/moveSpeed,
				rotationX:-( stage.stageHeight/2 - stage.mouseY )/moveSpeed,
				time:5,
				transition:"easeoutback"
			});
		}		
		
	}
}
